﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBModel;
using DBModel.DAL;

namespace CV_Projekt
{
    public partial class WorkDescription : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UppdateTable();

            //    if (!IsPostBack)
            //    {

            //        for (int i = 1; i <= 12; i++)
            //        {
            //            var nm = new System.Globalization.DateTimeFormatInfo();
            //            ddlmonth.Items.Add(new ListItem(nm.GetMonthName(i).ToString(), i.ToString()));

            //        }
            //        for (int i = 1950; i <= System.DateTime.Now.Year; i++)
            //        {
            //            ddlyear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //        }
            //    }


            //}

            //protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
            //{
            //    ddldate.Items.Clear();
            //    for (int i = 1; i <= System.DateTime.DaysInMonth(int.Parse(ddlyear.SelectedValue), int.Parse(ddlmonth.SelectedValue)); i++)
            //    {
            //        ddldate.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //    }

            //}
        }


        private void UppdateTable()
        {
            var work = Session["Work"] as WorkAdvertisement;
            if (work != null)
            {
                lblTitle.Text = work.WorkTitle.ToUpper();
                lblRefNummer.Text = work.WorkAdvertisementId.ToString();
                lblPublicerad.Text = work.PublishedDate.ToShortDateString();
                lblPlats.Text = work.WorkLocation;
                textboxJobbBeskrivning.Text = work.WorkDescription;
                lblTypAvAns.Text = work.EmploymentDurationInDays;
                lblLön.Text = work.SalaryType;
                lblKontakt.Text = work.ContactDescription;
                lblSistaAnsDag.Text = work.LastApplicationDate.ToShortDateString();
            }
        }

        protected void btnSök_Click(object sender, EventArgs e)
        {
            var user = Session["User"] as User;
            var work = Session["Work"] as WorkAdvertisement;
            if (user != null)
            {
                if (work != null)
                {
                    var workapplication = new WorkApplication()
                    {
                        ApplicantUserId = user.UserId,
                        WorkAdvertisementWorkAdvertisementId = work.WorkAdvertisementId
                    };

                    if (WorkApplicationDB.FindWorkApplication(workapplication.ApplicantUserId,
                        workapplication.WorkAdvertisementWorkAdvertisementId))
                    {
                        lblFeedback.Visible = true;
                        Session["Work"] = null;
                    }
                    else
                    {
                        WorkApplicationDB.InsertWorkApplication(workapplication);
                        Response.Redirect("UserApplicationMessage.aspx");
                    }
                }
            }
            else
            {
                {
                    lblFeedback.Text = "Du måste vara inlogad för att söka till tjänsten!";
                    lblFeedback.Visible = true;
                }
            }
           
        }
    }
}


    